﻿/*********************************************************
 * Copyright(c) 2012-2015
 * Author:wangzhonghai
 * description：
 *       1、执行时间
 * history：
 *       1、wangzhonghai - 2015/5/5 17:41:22 - Add 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AopAndIocDemo.Biz.Logging;
using PostSharp.Aspects;
using PostSharp.Extensibility;

namespace AopAndIocDemo.Biz.Aspect
{
    [Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method)]
    public sealed class TimmingAspect : OnMethodBoundaryAspect
    {
        private static readonly LoggerService LoggerService = new LoggerService();

        [NonSerialized] private Stopwatch _stopwatch = null;

        public override void OnEntry(MethodExecutionArgs args)
        {
            _stopwatch = Stopwatch.StartNew();
            base.OnEntry(args);
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            string msg = string.Format("[{0}] took {1}ms to execute.", new StackTrace().GetFrame(1).GetMethod().Name,
                _stopwatch.ElapsedMilliseconds);
            //LoggHelper.WriteLog(LogType.Trace, msg);
            LoggerService.Info(msg);

            base.OnExit(args);
        }
    }
}